package com.powernode.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.powernode.clients.SearchProductServiceClient;
import com.powernode.domain.Prod;
import com.powernode.domain.ProdTagReference;
import com.powernode.service.SearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SearchServiceImpl implements SearchService {
    @Autowired
    private SearchProductServiceClient searchProductServiceClient;

    @Override
    public List<Prod> prodListByTagId(Long tagId, Integer size) {
        // 1. 远程调用查询商品tag和商品的引用页
        Page<ProdTagReference> prodTagReferencePage = searchProductServiceClient.selectProdTagReferencePageByTagIdAndSize(tagId, size);
        if (prodTagReferencePage == null) {
            throw new RuntimeException("服务器开小差了");
        }

        // 2. 获取商品tag和引用列表
        List<ProdTagReference> prodTagReferenceList = prodTagReferencePage.getRecords();


        // 3. 获取商品ids
        List<Long> ids = prodTagReferenceList.stream().map(ProdTagReference::getProdId).collect(Collectors.toList());

        // 4. 调用远程接口获取商品对象集合
        List<Prod> prodList = searchProductServiceClient.selectProdListByIds(ids);
        if (prodList == null) {
            throw new RuntimeException("服务器开小差了");
        }

        return prodList;
    }
}
